#include <boost/foreach.hpp>
#include <skyroad/service/business_service.hpp>

business_service::business_service(bus& bus) : service(bus) 
{

}

business_service::~business_service()
{
}

void business_service::start()
{
    m_active = true;
    for (int i = 0; i < m_max_threads; i++)
    {
       m_worker_threads.push_back(new boost::thread(boost::bind(&business_service::run, this)));
    }
}

void business_service::stop()
{
    m_active = false;
//     BOOST_FOREACH(boost::thread* thr, m_worker_threads)
//     {
//         assert(thr != NULL);
//         thr->interrupt();
//     }

    BOOST_FOREACH(boost::thread* thr, m_worker_threads)
    {
        assert(thr != NULL);
        thr->join();
    }

    BOOST_FOREACH(boost::thread* thr, m_worker_threads)
    {
        assert(thr != NULL);
        delete thr;
    }
}

void business_service::run()
{
    while (m_active)
    {
        message req;
        m_in.wait(req);
        handle_request(req);
    }
}
